﻿using System.Collections.Specialized;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Services.Interfaces.Netflix;
using Rodunu.Movies.Services.Models.Netflix;
using Rodunu.Movies.Services.NetFlix.DataProvider;
using System.IO;

namespace Rodunu.Movies.Services.NetFlix.Logic
{
    public class ManageTitle : NetflixServiceBase, IManageTitle
    {
        #region Constructors

        public ManageTitle(string tokenKey)
            : base(tokenKey)
        {
            
        }

        public ManageTitle()
        {

        }

        public ManageTitle(UserInfo userId)
            : base(userId)
        {

        }

        #endregion

        #region IManageTitle Members

        public CatalogTitle GetTitle(string url)
        {
            var parameters = new NameValueCollection();
            return (Models.Netflix.CatalogTitle)
                _serializationHelper.FromXml(typeof(Models.Netflix.CatalogTitle),
                                            new StreamReader(_netflixService.GetUnprotected(url, parameters).GetResponseStream()).ReadToEnd());
        }

        public CatalogList GetSimilars(string url, int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            return (Models.Netflix.CatalogList)
                _serializationHelper.FromXml(typeof(Models.Netflix.CatalogList),
                                            new StreamReader(_netflixService.GetUnprotected(url, parameters).GetResponseStream()).ReadToEnd());
        }

        public PersonList GetPeopleInCatalog(string name, string movieId, int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            parameters.Add("term", name);
            parameters.Add("start_index", startIndex.ToString());
            parameters.Add("max_results", maxResults.ToString());
            return (PersonList)
                _serializationHelper.FromXml(typeof(PersonList),
                                            new StreamReader(
                                                _netflixService.GetUnprotected("http://api.netflix.com/catalog/people/" + movieId,
                                                                               parameters).GetResponseStream()).
                                                ReadToEnd());
        }

        public CatalogList GetSeries(string url, int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            return (Models.Netflix.CatalogList)
                _serializationHelper.FromXml(typeof(Models.Netflix.CatalogList),
                                            new StreamReader(_netflixService.GetUnprotected(url, parameters).GetResponseStream()).ReadToEnd());
        }

        public CatalogList GetEpisodes(string url, int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            return (Models.Netflix.CatalogList)
                _serializationHelper.FromXml(typeof(Models.Netflix.CatalogList),
                                            new StreamReader(_netflixService.GetUnprotected(url, parameters).GetResponseStream()).ReadToEnd());
        }

        public FormatAvailabilityList GetFormatAvailibility(string url)
        {
            var parameters = new NameValueCollection();
            return (FormatAvailabilityList)
                _serializationHelper.FromXml(typeof(FormatAvailabilityList),
                                            new StreamReader(_netflixService.Get(url, parameters).GetResponseStream()).ReadToEnd());
        }

        public TitleStates GetTitleStates(string url)
        {
            var parameters = new NameValueCollection();
            return (TitleStates)
                   _serializationHelper.FromXml(typeof (TitleStates),
                                                new StreamReader(
                                                    _netflixService.Get(url, parameters).GetResponseStream()).ReadToEnd());
        }

        #endregion
    }
}
